package org.example.sensortest;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends Activity implements SensorEventListener, OnItemSelectedListener
{
	private static final double TILT_ACTIVE = 0.2;
	private static final double TILT_FLAT = 0.1;
	
	private Sensor sensor = null;
	private SensorManager sensorManager;
	private int stateX = 0;
	private int stateY = 0;
	private int stateZ = 0;
	private TextView statusX, statusY, statusZ;
	private SensorAdapter adapter;
	private Spinner spinner;
	private LinearLayout values;
	
	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy)
	{}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		
		sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		
		// sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		// sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
		// sensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
		// sensor = sensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION);
		sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
		
		adapter = new SensorAdapter(this);
		
		View elem = findViewById(R.id.spinnerSensors);
		spinner = (Spinner) elem;
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
		
		if(adapter.isEnabled(1))
			spinner.setSelection(1);
		
		values = (LinearLayout) findViewById(R.id.listValues);
		
		statusX = (TextView) findViewById(R.id.status_x);
		statusY = (TextView) findViewById(R.id.status_y);
		statusZ = (TextView) findViewById(R.id.status_z);
	}
	
	@Override
	protected void onPause()
	{
		super.onPause();
		
		unregister();
	}
	
	private void unregister()
	{
		if(sensor != null)
			sensorManager.unregisterListener(this);
		
		values.removeAllViews();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		register();
	}
	
	private void register()
	{
		if(sensor != null)
			sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
	}
	
	@Override
	public void onSensorChanged(SensorEvent event)
	{
		evaluteXYZ(event);
		
		int shouldLength = event.values.length;
		while(shouldLength < values.getChildCount())
			values.removeView(values.getChildAt(shouldLength));
		while(shouldLength > values.getChildCount())
			values.addView(new SensorView(this));
		
		for(int i = 0; i < event.values.length; i++)
			((SensorView) values.getChildAt(i)).setValue(event.values[i]);
	}
	
	private void evaluteXYZ(SensorEvent event)
	{
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		
		if(Math.abs(x) < TILT_FLAT && stateX != 0)
		{
			stateX = 0;
			Log.v("sensortest", "tilted x flat");
			statusX.setText("tilted x flat");
		}
		if(x > TILT_ACTIVE && stateX != 1)
		{
			stateX = 1;
			Log.v("sensortest", "tilted x forward");
			statusX.setText("tilted x forward");
		}
		if(-x > TILT_ACTIVE && stateX != -1)
		{
			stateX = -1;
			Log.v("sensortest", "tilted x backward");
			statusX.setText("tilted x backward");
		}
		
		if(Math.abs(y) < TILT_FLAT && stateY != 0)
		{
			stateY = 0;
			Log.v("sensortest", "tilted y flat");
			statusY.setText("tilted y flat");
		}
		if(y > TILT_ACTIVE && stateY != 1)
		{
			stateY = 1;
			Log.v("sensortest", "tilted y right");
			statusY.setText("tilted y right");
		}
		if(-y > TILT_ACTIVE && stateY != -1)
		{
			stateY = -1;
			Log.v("sensortest", "tilted y left");
			statusY.setText("tilted y left");
		}
		
		if(Math.abs(z) < TILT_FLAT && stateZ != 0)
		{
			stateZ = 0;
			Log.v("sensortest", "tilted z flat");
			statusZ.setText("tilted z flat");
		}
		if(z > TILT_ACTIVE && stateZ != 1)
		{
			stateZ = 1;
			Log.v("sensortest", "tilted z left");
			statusZ.setText("tilted z left");
		}
		if(-z > TILT_ACTIVE && stateZ != -1)
		{
			stateZ = -1;
			Log.v("sensortest", "tilted z right");
			statusZ.setText("tilted z right");
		}
	}
	
	@Override
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
	{
		unregister();
		sensor = ((SensorAdapter.ItemSensor) (adapter.getItem(position))).getSensor();
		register();
	}
	
	@Override
	public void onNothingSelected(AdapterView<?> parent)
	{
		if(sensor != null)
			sensorManager.unregisterListener(this);
		sensor = null;
	}
}
